//
//  DeviceTableViewCell.m
//  ZhongKeXinHeater
//
//  Created by Kendama on 2017/6/2.
//  Copyright © 2017年 DT. All rights reserved.
//

#import "DeviceTableViewCell.h"
@interface DeviceTableViewCell ()

/** <#Description#> **/
@property(nonatomic,strong) UILabel *nameLabel;
@property(nonatomic,strong) UIImageView *stateIV;

@end

@implementation DeviceTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self masLayoutSubviews];
    }
    return self;
}

- (void)setDeivce:(DTDevice *)deivce {
    self.nameLabel.text = deivce.deviceName;
    if (deivce.isOnline) {
        _stateIV.image = [UIImage imageNamed:@"device_on"];
    } else {
        _stateIV.image = [UIImage imageNamed:@"device_off"];
    }
}

- (void)masLayoutSubviews{
    __weak UIView *superView = self.contentView;
    UIImageView *arrow = [[UIImageView alloc] init];
    [superView addSubview:arrow];
    arrow.image = [UIImage imageNamed:@"icon_Forward"];
    [arrow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.right.equalTo(self).with.offset(-px1080Width(46));
        make.width.mas_equalTo(px1080Width(30));
        make.height.mas_equalTo(px1920Height(50));
    }];
    
    [self.stateIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(superView.mas_centerY);
        make.right.equalTo(arrow.mas_left).with.offset(-px1080Width(46));
        make.height.mas_equalTo(px1920Height(50));
        make.width.mas_equalTo(px1080Width(112));
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(superView.mas_centerY);
        make.left.mas_equalTo(superView.mas_left).with.offset(px1080Width(46));
        make.height.mas_equalTo(px1920Height(45));
        make.right.equalTo(self.stateIV.mas_left).with.offset(-px1080Width(20));
    }];
}

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"DeviceTableViewCell";
    id cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}

- (UILabel *)nameLabel
{
    if(!_nameLabel)
    {
        UIView *superView = self.contentView;
        _nameLabel = [[UILabel alloc]init];
        [superView addSubview:_nameLabel];
        [_nameLabel setFont:[UIFont systemFontOfSize:px1080Width(45)]];
//        [_nameLabel setTextColor:<#(UIColor * _Nullable)#>];
        _nameLabel.text = @"设备1";
    }
    return _nameLabel;
}

- (UIImageView *)stateIV
{
    if(!_stateIV)
    {
        UIView *superView = self.contentView;
        _stateIV = [[UIImageView alloc]init];
        [superView addSubview:_stateIV];
        _stateIV.image = [UIImage imageNamed:@"device_on"];
    }
    return _stateIV;
}

//- (void)layoutSubviews {
//    for (UIView *subView in self.subviews) {
//        if([subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]) {
//            
//            // 拿到subView之后再获取子控件
//            
//            // 因为上面删除按钮是第二个加的所以下标是1
//            UIView *deleteConfirmationView = subView.subviews[1];
//            //改背景颜色
//            deleteConfirmationView.backgroundColor = [UIColor grayColor];
//            for (UIView *deleteView in deleteConfirmationView.subviews) {
//                NSLog(@"%@",deleteConfirmationView.subviews);
//                UIImageView *deleteImage = [[UIImageView alloc] init];
//                deleteImage.contentMode = UIViewContentModeScaleAspectFit;
//                deleteImage.image = [UIImage imageNamed:@"icon_edit"];
////                deleteImage.frame = CGRectMake(0, 0, deleteView.frame.size.width, deleteView.frame.size.height);
//                [deleteView addSubview:deleteImage];
//                [deleteImage mas_makeConstraints:^(MASConstraintMaker *make) {
//                    make.center.mas_equalTo(deleteView);
//                    make.height.mas_equalTo(deleteView.frame.size.height);
//                    make.width.mas_equalTo(deleteView.frame.size.width);
//                }];
//            }
//            
//            // 这里是右边的
//            UIView *shareConfirmationView = subView.subviews[0];
//            shareConfirmationView.backgroundColor = [UIColor blackColor];
//            for (UIView *shareView in shareConfirmationView.subviews) {
//                UIImageView *shareImage = [[UIImageView alloc] init];
//                shareImage.contentMode = UIViewContentModeScaleAspectFit;
//                shareImage.image = [UIImage imageNamed:@"icon_delete"];
////                shareImage.frame = CGRectMake(0, 0, shareView.frame.size.width, shareView.frame.size.height);
//                [shareView addSubview:shareImage];
//                [shareImage mas_makeConstraints:^(MASConstraintMaker *make) {
//                    make.center.mas_equalTo(shareView);
//                    make.height.mas_equalTo(shareView.frame.size.height);
//                    make.width.mas_equalTo(shareView.frame.size.width);
//                }];
//            }
//        }
//    }
//}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
